<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import Edit from "./../edit/index";
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataCoupon: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataCoupon()),
  },
  data() {
    return {
      appWindow: window,
      form: {
        cost_switch: 1,
        cost: {},
        lottery: {
          win_number: null,
          // 是否开启抽奖，0：关闭，1开启
          is_open: 1,
          // 是否应用独立规则，0：应用，1不应用
          is_separate_rules: 0,
          // 团长抽奖设置，0：无限制，1：必中，2：必不中
          team_leader_set: 0,
          // 新成员抽奖设置，0：无限制，1：必中，2：必不中
          team_new_member_set: 1,
          // 中奖奖励
          winning_reward: [],
          // 未中奖补偿
          loser_reward: [],
        },
      },
      couponsIds: [],
      couponsIds2: [],
      load: true,

      myList: [],
    };
  },
  methods: {
    // 验证 
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
            valid ? resolve(true) : resolve(false)
        });
      });
    },
    // 会员奖励/非会员奖励的删除
    mydel(data, my_type) {
      console.log(data.name, my_type);
      let _d = [];
      if(my_type == 'yes') {
        this.form.lottery.winning_reward.forEach(item => {
          if(item.name != data.name) {
            _d.push(item);
          }
        });
        this.form.lottery.winning_reward = _d;
      }else if(my_type == 'no') {
        this.form.lottery.loser_reward.forEach(item => {
          if(item.name != data.name) {
            _d.push(item);
          }
        });
        this.form.lottery.loser_reward = _d;
      }
    },
    edit(type) {
      Edit.init({
        type: type,
        onSuccess: (res) => {
          if(type == 'yes') {
            this.form.lottery.winning_reward = res;
          } else if(type == 'no') {
            this.form.lottery.loser_reward = res;
          }else if(type == 'select'){
            this.form.cost = res[0];
          }
        }
      })
    },
    async getCoupon(type) {
      this.$vFN.Directives.SelectCoupon({
        // count: 1,
        appWindow: this.appWindow,
        clientType: "platform",
        couponIds: type == "member_no" ? this.form.lottery.loser_reward.coupon : this.form.lottery.winning_reward.coupon,
        apiConfig: this.$vFN.Tools.getAppGlobalData('selectCoupon.couponsList', this.appWindow),
        onSuccess: (ids) => {
          if(type == "member_yes") {
            this.form.lottery.winning_reward.coupon = ids;
          }else {
            this.form.lottery.loser_reward.coupon = ids;
          }
        }
      })
    },
    // 获取修改设置后的数据
    async getData(type) {
      let res = await this.$vFN.Http.post(
        { 
          url: "admin/set/getBase",
        },
        window
      );
      
      if(res.data.lottery != null) {
        // 正常拿到数据回显
        this.form.lottery = res.data.lottery;
        if(typeof this.form.lottery.winning_reward == "string" || typeof this.form.lottery.loser_reward == "string") {
          this.form.lottery.winning_reward = JSON.parse(this.form.lottery.winning_reward);
          this.form.lottery.loser_reward = JSON.parse(this.form.lottery.loser_reward);
        }
        this.form.lottery.winning_reward.forEach(item => {
          if(item.type == 'select') {
            this.couponsIds = item.award;
          }
          
        });
        this.form.lottery.loser_reward.forEach(item => {
          if(item.type == 'select') {
            this.couponsIds2 = item.award;
          }
        });
      }
      this.load = false;
    },
    // 提交修改
    async sum() {
      // console.log(this.form.lottery.winning_reward,this.form.lottery.loser_reward);
      // if(this.form.lottery.win_number == null || this.form.lottery.win_number == "") {
      //   this.$vFN.Tools.messageError("中奖人数不能为空");
      //   return
      // }
      if(this.form.cost_switch == 1 && this.form.cost == null) {
        this.$vFN.Tools.messageError("选择要扣除的资产");
        return;
      }
      if (!await this.validate()) {
        return
      }
      if(this.form.lottery.is_open == 1 && (JSON.stringify(this.form.lottery.winning_reward) == "[]" || this.form.lottery.loser_reward == "[]")){
        this.$vFN.Tools.messageError("选择抽中会员的奖励和未抽中会员的奖励！");
        return
      }else {
        this.form.lottery.winning_reward.forEach(item => {
          if(item.type == 'select') {
            item.award = this.couponsIds;
          }
        });
        this.form.lottery.loser_reward.forEach(item => {
          if(item.type == 'select') {
            item.award = this.couponsIds2;
          }
        });
      }
      
      this.load = true;
      let res = await this.$vFN.Http.post(
        {
          url: "admin/set/setBase",
          data: this.form,
        },
        window
      );
      
      if(res.code == 200) {
        this.getData()
        this.$vFN.Tools.messageSuccess("设置成功");
      }
      
    },

    // 初始化
    async init() {
      this.getData();

    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>